import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { viteSingleFile } from 'vite-plugin-singlefile';
import legacy from '@vitejs/plugin-legacy';

// https://vite.dev/config/
export default defineConfig(({ mode, command }) => {
    console.log(`---当前环境：【${mode}】---`);
    return {
        base: './',
        plugins: [
            vue(),
            AutoImport({
                resolvers: [VantResolver()],
            }),
            Components({
                resolvers: [VantResolver()],
            }),
            legacy({
                targets: ['defaults', 'not IE 11'],
            }),
            // viteSingleFile(),
            // vueDevTools(),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url)),
            },
        },
        server: {
            host: '0.0.0.0',
            port: 5000,
            open: true,
            proxy: {
                // '/api': {
                //     target: 'http://localhost:8080',
                //     // rewrite: (path: string) => path.replace(/\/api/, ''),
                //     configure: (proxy: any, options: any) => {
                //         // 配置此项可在响应头中看到请求的真实地址
                //         proxy.on('proxyRes', (proxyRes: any, req: any) => {
                //             proxyRes.headers['x-real-url'] = new URL(req.url || '', options.target)?.href || '';
                //         });
                //     },
                // },
                // '/rest': 'https://twines-web.smartapp.net.cn/WebApi',
            },
        },
    };
});
